<template>
  <div>

    <el-form :inline="true" >

      <el-form-item>
      <el-input v-model="searchForm.name" aria-placeholder="名称" clearable></el-input>
      </el-form-item>

      <el-form-item>
        <el-button @click="getRoleList">搜索</el-button>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="dialogVisible = true">新增</el-button>
      </el-form-item>

      <el-form-item>
        <el-popconfirm
          confirm-button-text='是的'
          cancel-button-text='不用了'
          icon="el-icon-info"
          icon-color="red"
          title="确定要批量删除吗？"
          @confirm="delHandle(null)"
        >
          <el-button type="danger" slot="reference" :disabled="delBtlStatu">批量删除</el-button>
        </el-popconfirm>
      </el-form-item>


    </el-form>


    <!--数据表格-->
    <el-table
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
      border
      stripe
      @selection-change="handleSelectionChange">

      <el-table-column
        type="selection"
        width="55">
      </el-table-column>

      <el-table-column
        prop="name"
        label="名称"
        width="120">
      </el-table-column>

      <el-table-column
        prop="code"
        label="唯一编码"
        show-overflow-tooltip>
      </el-table-column>

      <el-table-column
        prop="remark"
        label="描述"
        show-overflow-tooltip>
      </el-table-column>

      <el-table-column
        prop="statu"
        label="状态">
        <template slot-scope="scope">
          <el-tag size="small" v-if="scope.row.statu === 1" type="success">正常</el-tag>
          <el-tag size="small" v-else-if="scope.row.statu === 0" type="danger">禁用</el-tag>
        </template>
      </el-table-column>

      <el-table-column
        prop="control"
        label="操作">
        <template slot-scope="scope">

          <el-button type="text" @click="permHandle(scope.row.id)">分配权限</el-button>

          <el-divider direction="vertical"></el-divider>

          <el-button type="text" @click="editHandle(scope.row.id)">编辑</el-button>

          <el-divider direction="vertical"></el-divider>

          <el-popconfirm
            confirm-button-text='好的'
            cancel-button-text='不用了'
            icon="el-icon-info"
            icon-color="red"
            title="这是一段内容确定删除吗？"
            @confirm="delHandle(scope.row.id)"
          >
            <el-button type="text" slot="reference">删除</el-button>
          </el-popconfirm>

        </template>
      </el-table-column>

    </el-table>

    <!--显示分页-->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      layout="total, sizes, prev, pager, next, jumper"
      :current-page="current"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="size"
      :total="total">
    </el-pagination>

    <!--编辑时新增对话框-->
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="550px"
      :before-close="handleClose">
      <el-form :model="editForm" :rules="editFormRules" ref="editForm" label-width="100px" class="demo-editForm">

        <el-form-item label="角色名称" prop="name" label-width="100px">
          <el-input v-model="editForm.name" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="唯一编码" prop="code" label-width="100px">
          <el-input v-model="editForm.perms" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="描述" prop="remark" label-width="100px">
          <el-input v-model="editForm.icon" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="状态" prop="statu" label-width="100px">
          <el-radio-group v-model="editForm.statu">
            <el-radio :label=0>禁用</el-radio>
            <el-radio :label=1>正常</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('editForm')">立即创建</el-button>
          <el-button @click="resetForm('editForm')">重置</el-button>
        </el-form-item>

      </el-form>
    </el-dialog>

    <!--分配权限时新增对话框-->
    <el-dialog
      title="分配权限"
      :visible.sync="permDialogVisible"
      width="550px">

      <el-form :model="permForm">
        <el-tree
          :data="permTreeData"
          show-checkbox
          refs="permTree"
          :default-expand-all=true
          :check-strictly=true
          node-key="id"
          :props="defaultProps">
        </el-tree>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="permTreeData = false">取 消</el-button>
        <el-button type="primary" @click="submitPermFormHandle('permForm')">确 定</el-button>
      </span>

    </el-dialog>
  </div>
</template>

<script>
  import {delRoleHandle, editRoleHandle, getRoleList, submitRoleForm, getMenuTree, permFormHandle} from "../../network/request";

    export default {
        name: "Role",
      data() {
          return {
            searchForm: {},
            delBtlStatu: true,
            total: 0,
            size: 10,
            current: 1,
            dialogVisible: false,
            permDialogVisible: false,
            editForm: {},
            permForm: {},
            tableData: [],
            editFormRules: {
              name: [{required: true, message: '请输入名称', trigger: 'blur'}],
              code: [{required: true, message: '请输入唯一编码', trigger: 'blur'}],
              statu: [{required: true, message: '请选择状态', trigger: 'blur'}]
            },
            multipleSelection: [],
            defaultProps: {
              children: 'children',
              label: 'name'
            },
            permTreeData: []
          }
      },
      created() {
          this.getRoleList();
          getMenuTree().then(res => {
            this.permTreeData = res.data.data
          })
      },
      methods: {
        toggleSelection(rows) {
          if (rows) {
            rows.forEach(row => {
              this.$refs.multipleTable.toggleRowSelection(row);
            });
          } else {
            this.$refs.multipleTable.clearSelection();
          }
        },
        handleSelectionChange(val) {
          this.multipleSelection = val;
          this.delBtlStatu = val.length == 0
        },
        handleSizeChange(val) {
          this.size = val
          this.getRoleList()
        },
        handleCurrentChange(val) {
          this.current = val
          this.getRoleList()
        },
        getRoleList() {
          getRoleList(
            {
              params:{
                name: this.searchForm.name,
                current: this.current,
                size: this.size
              }
            }
            ).then(res => {
            this.tableData = res.data.data.records
            this.size = res.data.data.size
            this.current = res.data.data.current
            this.total = res.data.data.total

          })
        },
        submitForm(formName) {
          this.$refs[formName].validate((valid) => {
            if (valid) {
              submitRoleForm((this.editForm.id ? "update" : "save") ,this.editForm).then(res => {
                this.$message({
                  showClose: true,
                  message: '恭喜你，操作成功',
                  type: 'success',
                  onClose: () => {
                    this.getRoleList()
                  }
                });
                this.dialogVisible = false
              })
            } else {
              console.log('error submit!!');
              return false;
            }
          });
        },
        editHandle(id) {
          editRoleHandle(id).then( res => {
            this.editForm = res.data.data
            this.dialogVisible = true
          })
        },
        resetForm(formName) {
          this.$refs[formName].resetFields();
          this.dialogVisible = false
          this.editForm = {}
        },
        delHandle(id) {
          var ids = []
          if (id){
            ids.push(id)
          }else {
            this.multipleSelection.forEach(row => {
              ids.push(row.id)
            })
          }
          delRoleHandle(ids).then( res => {
            this.$message({
              showClose: true,
              message: '恭喜你，操作成功',
              type: 'success',
              onClose: () => {
                this.getRoleList()
              }
            });
          })
        },
        handleClose() {
          this.resetForm('editForm')
        },
        permHandle(id) {
          this.permDialogVisible = true
          editRoleHandle(id).then(res => {
            this.$refs.permTree.setCheckedKeys(res.data.data.menuIds);
            this.permForm = res.data.data
          })
        },
        submitPermFormHandle() {
          var menuIds = this.$refs.permTree.getCheckedKeys()
          permFormHandle(this.permForm.id , menuIds).then(res => {
            this.$message({
              showClose: true,
              message: '恭喜你，操作成功',
              type: 'success',
              onClose: () => {
                this.getRoleList()
              }
            });
            this.permDialogVisible = false
          })
        }
      }
    }
</script>

<style scoped>

  .el-pagination{
    float: right;
    margin-top: 22px;
  }

</style>
